﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
namespace PhoneInfo.Page
{
    public partial class ShrewdEye : PhoneApplicationPage
    {

        ShrewdEyeView shrewd;
        //pivot 标题的数组，标题不放置内容为了让标题更多；
        public ShrewdEye()
        {
            InitializeComponent();
            shrewd=new ShrewdEyeView();
            shrewd.IsLoadSuccess=0;
            //AppBar

            if (App.appSitting.Theme == 1)
            {
                ApplicationBar = (ApplicationBar)this.Resources["AppBar"];
            }
            else
            {
                ApplicationBar = (ApplicationBar)this.Resources["AppBar1"];
            }
          
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //在这里进行处理
            shrewd.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(shrewd.GetPhoneInfoUrl()));
            shrewd.PropertyChanged+=new System.ComponentModel.PropertyChangedEventHandler(shrewd_PropertyChanged);
           
        }
        void shrewd_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(sender==null)
                 return;
            ShrewdEyeView se = (ShrewdEyeView)sender;
            if (e.PropertyName == "IsLoadSuccess")
            {
                //进行处理
                if (se.IsLoadSuccess==2)
                {
                   pivot.DataContext = shrewd;
                   ProgBar.Visibility = Visibility.Collapsed;
                   PivotLocationView.Source = pivot;
                }
                else if (se.IsLoadSuccess == 1 || se.IsLoadSuccess==-1)
                {
                   // pivot.DataContext = shrewd;
                    ProgBar.Visibility = Visibility.Collapsed;
                    ToastPrompt toast = new ToastPrompt()
                    {
                        Message="网络异常，请稍后重试！"
                    };
                    toast.Show();
                   // PivotLocationView.Source = pivot;
                }
                
            }
        }
        private void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender==null)
            {
                return;
            }

            string objective = "/Page/ContentAndReply.xaml";
            if (shrewd.ShrewdEyeItemS.Count > 0 && pivot.SelectedIndex >= 0)
            {
                objective += "?topicid=" + shrewd.ShrewdEyeItemS[pivot.SelectedIndex].Topicid;
                objective += "&readcount=" + shrewd.ShrewdEyeItemS[pivot.SelectedIndex].Readcount;
                objective += "&replycount=" + shrewd.ShrewdEyeItemS[pivot.SelectedIndex].Replycount;
                objective += "&title=" + shrewd.ShrewdEyeItemS[pivot.SelectedIndex].Title;
                objective += "&ShrewdEye=" + "ShrewdEye";
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
            }
            else
            {
                if (shrewd.IsLoadSuccess == 2)
                {
                  MessageBox.Show("数据有错误，请刷新后再试");
                }
            }
           
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void mianPage(object sender, EventArgs e)
        {
            string objective = "/Page/main.xaml";
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }

        private void refresh(object sender, EventArgs e)
        {
            ProgBar.Visibility = Visibility.Visible;
            shrewd = new ShrewdEyeView();
            shrewd.IsLoadSuccess = 0;
            shrewd.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(shrewd.GetPhoneInfoUrl()));
            shrewd.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(shrewd_PropertyChanged);
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            string pageName = "/Page/main.xaml";
            NavigationService.Navigate(new Uri(pageName, UriKind.Relative));
            //if (App.appSitting.IsReturnRrompt == true)
            //{
            //    MessageBoxResult result = MessageBox.Show("是否退出手机大全", "提示", MessageBoxButton.OK | MessageBoxButton.OKCancel);
            //    if (result == MessageBoxResult.OK)
            //    {
            //        // base.OnBackKeyPress(e);
            //    }
            //    else
            //    {
            //        e.Cancel = true;
            //        return;
            //    }
            //}
            ////
            //if (App.appSitting.IsReturnClear == true)
            //{
            //    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            //    {
            //        string filePath = "Xmls";
            //        String[] files = store.GetFileNames(filePath);
            //        for (int j = 0; j < files.Length; j++)
            //        {
            //            String fPath = filePath + "\\" + files[j];
            //            store.DeleteFile(fPath);
            //        }
            //    }
            //}
            //base.OnBackKeyPress(e);
        }
    }
}